package com.blb.service.admin.impl;

import com.blb.dao.admin.CarouselDao;
import com.blb.dao.admin.impl.CarouselDaoImpl;
import com.blb.pojo.Carousel;
import com.blb.pojo.Page;
import com.blb.service.admin.ICarouselService;
import com.blb.utils.ApplicationContextUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CarouselServiceImpl implements ICarouselService {

    @Autowired
    private CarouselDao carouselDao;

    @Override
    public Page<Carousel> page(int page, int size) throws Exception {
        List<Carousel> list = carouselDao.selectPage(page, size);
        int count = carouselDao.count();
        return new Page<Carousel>(page, size, list, count);
    }

    @Override
    public void add(Carousel carousel) throws Exception {
        System.out.println(carousel.getImageUrl());
        if (carousel.getImageUrl() == "" || carousel.getImageUrl() == null){
            carousel.setImageUrl("beautifulGirl.png");
        }
        carouselDao.insert(carousel);
    }

    @Override
    public void deleteById(int id) throws Exception {
        carouselDao.deleteById(id);
    }

    @Override
    public void batchDelete(String[] ids) throws Exception {
        carouselDao.batchDelete(ids);
    }

    @Override
    public Carousel getById(int id) throws Exception {
        return carouselDao.selectById(id);
    }

    @Override
    public void update(Carousel carousel) throws Exception {
        Carousel carousel1 = carouselDao.selectBySeq(carousel.getSeq());
        if (carousel1 != null && carousel1.getId()!=carousel.getId()){
            throw new Exception("此顺序已经存在");
        }
        carouselDao.update(carousel);
    }
}
